
package com.weed.client.login;



import com.weed.client.components.util.*;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;

import java.awt.Image;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.MouseListener;
import java.io.FileInputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Properties;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.border.LineBorder;

/**
 * 背景使用纹理。
 * @author Administrator
 */
public class HeadButton extends javax.swing.JLabel implements MouseListener{
    BufferedImage backgroundImage = null;//背景图位置
    BufferedImage insetsImage = null;//连框图
    BufferedImage mouseEnterImage = null;//鼠标移入图
     
    protected BufferedImage bi = null;//缓存图象
    protected BufferedImage insetsBi = null;//缓存图象
    
    private String skinIndex="headbutton";
    protected int width =-1;
    protected int height=-1;

    private String code;
    private String parentcode;
    private String parentLevel;
    private String actionString;
    
    public HeadButton() {
        super();
        load();
    }
    

    public HeadButton(String skinIndex) {
        super();
        this.skinIndex = skinIndex;
        load();

    }
    
    public Dimension getPreferredSize(){
        Dimension dim = super.getPreferredSize();
        if(width != -1){
            dim.width = width;
        }
        if(height != -1){
            dim.height = height;
        }
        return dim;
    }
    public Dimension getMinimumSize(){
        Dimension dim = super.getMinimumSize();
        if(width != -1){
            dim.width = width;
        }
        if(height != -1){
            dim.height = height;
        }
        
        return dim;
    }
     public Dimension getMaximumSize(){
        Dimension dim = super.getMaximumSize();
        if(width != -1){
            dim.width = width;
        }
        if(height != -1){
            dim.height = height;
        }
        return dim;
    }
     
    protected void load(){
       Properties prop = SkinUtil.getSkinFile();
       SkinUtil.setFont(prop.getProperty(getSkinIndex()+".font"),this);
       SkinUtil.setColor(prop.getProperty(getSkinIndex()+".background"), this, "back");
       SkinUtil.setColor(prop.getProperty(getSkinIndex()+".foreground"), this, "fore");
       SkinUtil.setBorder(prop.getProperty(getSkinIndex()+".border"),this);
       backgroundImage= SkinUtil.getBufferedImage(prop.getProperty(getSkinIndex()+".backgroundimage"), this);
       mouseEnterImage= SkinUtil.getBufferedImage(prop.getProperty(getSkinIndex()+".mouseenterimage"), this);
       insetsImage = SkinUtil.getBufferedImage(prop.getProperty(getSkinIndex()+".borderImage"), this);
       width = SkinUtil.getWidth(prop.getProperty(getSkinIndex()+".width"), this);
       height = SkinUtil.getHeight(prop.getProperty(getSkinIndex()+".height"), this);
       
       if(backgroundImage!=null)
           bi = backgroundImage;
       
       this.addMouseListener(this);
    }
    public Insets getInsets(){
       Insets insets = super.getInsets();
       if(insets == null || (insets.bottom==0 && insets.left==0 && insets.right ==0 && insets.top ==0))
           return new Insets(1,1,1,1);
       return insets;
    }
    @Override
    public void paintComponent(Graphics g) {
        if(bi!=null)
            GraphicsUtils.drawImageWith9Grids(g, bi, 0, 0, this.getSize().width,this.getSize().height, this.getInsets(), true);
        if(insetsBi!=null)
            GraphicsUtils.drawImageWith9Grids(g, bi, 0, 0, this.getSize().width,this.getSize().height, this.getInsets(), false);
        super.paintComponent(g);
    }

    public void mouseClicked(MouseEvent e) {}

    public void mousePressed(MouseEvent e) {}

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        if(mouseEnterImage!=null){
            bi= mouseEnterImage;
            this.repaint();
        }
    }

    public void mouseExited(MouseEvent e) {
        if(backgroundImage!=null){
            bi =backgroundImage;
            this.repaint();
        }else{
            bi =null;
            this.repaint();
        }
    }

    /**
     * @return the skinIndex
     */
    public String getSkinIndex() {
        return skinIndex;
    }

    /**
     * @param skinIndex the skinIndex to set
     */
    public void setSkinIndex(String skinIndex) {
        this.skinIndex = skinIndex;
        load();
    }

    /**
     * @return the code
     */
    public String getCode() {
        return code;
    }

    /**
     * @param code the code to set
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * @return the parentcode
     */
    public String getParentcode() {
        return parentcode;
    }

    /**
     * @param parentcode the parentcode to set
     */
    public void setParentcode(String parentcode) {
        this.parentcode = parentcode;
    }

    /**
     * @return the parentLevel
     */
    public String getParentLevel() {
        return parentLevel;
    }

    /**
     * @param parentLevel the parentLevel to set
     */
    public void setParentLevel(String parentLevel) {
        this.parentLevel = parentLevel;
    }

    /**
     * @return the actionString
     */
    public String getActionString() {
        return actionString;
    }

    /**
     * @param actionString the actionString to set
     */
    public void setActionString(String actionString) {
        this.actionString = actionString;
    }

}
